package com.edu.system.service.impl;

import com.edu.system.domain.TCanteen;

import com.edu.system.domain.TClassroom;
import com.edu.system.service.ITCanteenService;
import com.edu.system.service.ITClassroomService;
import java.util.List;

import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.system.mapper.TTerminalMapper;
import com.edu.system.domain.TTerminal;
import com.edu.system.service.ITTerminalService;

/**
 * 机器管理Service业务层处理
 *
 * @author xxxx
 * @date 2023-12-21
 */
@Service
public class TTerminalServiceImpl implements ITTerminalService {
    @Autowired
    private TTerminalMapper tTerminalMapper;
    @Resource
    private ITClassroomService itClassroomService;
    @Resource
    private ITCanteenService itCanteenService;

    /**
     * 查询机器管理
     *
     * @param id 机器管理主键
     * @return 机器管理
     */
    @Override
    public TTerminal selectTTerminalById(Long id) {
        return tTerminalMapper.selectTTerminalById(id);
    }

    @Override
    public TTerminal selectTTerminalByCode(String code) {
        return tTerminalMapper.selectTTerminalByCode(code);
    }

    @Override
    public void batchOnOff(List<Long> ids, Integer status) {
        ids.forEach(id -> {
            TTerminal t = new TTerminal();
            t.setId(id);
            t.setStatus(status);
            tTerminalMapper.updateTTerminal(t);
        });
    }

    /**
     * 查询机器管理列表
     *
     * @param tTerminal 机器管理
     * @return 机器管理
     */
    @Override
    public List<TTerminal> selectTTerminalList(TTerminal tTerminal) {
        return tTerminalMapper.selectTTerminalList(tTerminal);
    }

    @Override
    public List<TTerminal> selectTTerminalListByIds(Long[] ids) {
        return tTerminalMapper.selectTTerminalListByIds(ids);
    }

    /**
     * 新增机器管理
     *
     * @param t 机器管理
     * @return 结果
     */
    @Override
    public TTerminal insertTTerminal(TTerminal t) {
        populateLocation(t);
        tTerminalMapper.insertTTerminal(t);
        return t;
    }

    private void populateLocation(TTerminal t) {
        if (t.getType() == null || t.getLocationId() == null) {
            return;
        }
        if (t.getType().equals("班牌")){
            TClassroom classroom = itClassroomService.selectTClassroomById(t.getLocationId());
            t.setLocation(classroom.getName());
        } else {
            TCanteen tCanteen = itCanteenService.selectTCanteenById(t.getLocationId());
            t.setLocation(tCanteen.getName());
        }
    }

    /**
     * 修改机器管理
     *
     * @param tTerminal 机器管理
     * @return 结果
     */
    @Override
    public int updateTTerminal(TTerminal tTerminal) {
        populateLocation(tTerminal);
        return tTerminalMapper.updateTTerminal(tTerminal);
    }

    /**
     * 批量删除机器管理
     *
     * @param ids 需要删除的机器管理主键
     * @return 结果
     */
    @Override
    public int deleteTTerminalByIds(Long[] ids) {
        return tTerminalMapper.deleteTTerminalByIds(ids);
    }

    /**
     * 删除机器管理信息
     *
     * @param id 机器管理主键
     * @return 结果
     */
    @Override
    public int deleteTTerminalById(Long id) {
        return tTerminalMapper.deleteTTerminalById(id);
    }

    @Override
    public Map<String, Long> codeIdMapByLocationIds(List<Long> locationIds, String terminalType) {
        TTerminal terminal = new TTerminal();
        terminal.setType(terminalType);
        if (CollectionUtils.isNotEmpty(locationIds)){
            terminal.setLocationIds(locationIds);
        }
        List<TTerminal> tTerminals = this.selectTTerminalList(terminal);
        return tTerminals.stream().collect(Collectors.toMap(TTerminal::getCode, TTerminal::getLocationId));
    }
}
